package com.customers.dao.Impl;

import com.customers.dao.UserDao;
import com.customers.entity.Message;
import com.customers.entity.User;
import com.customers.util.ConnectJDBC;
import com.customers.util.EncapsulationUtil;
import com.customers.util.XmlReadUtil;
import org.dom4j.Element;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class UserDaoImpl implements UserDao {
    private Map<String, String> sqlMap;

    @Override
    public String toString() {
        return "UserDaoImpl{" +
                "sqlMap=" + sqlMap +
                '}';
    }

    public Map<String, String> getSqlMap() {
        return sqlMap;
    }

    public void setSqlMap(Map<String, String> sqlMap) {
        this.sqlMap = sqlMap;
    }

    @Override
    public User searchUserByName(User user, String sql) throws SQLException {
        ResultSet rs = null;
//        String sql = "select * from tb_user where username = ? ";
        PreparedStatement ps = null;
        User u = new User();
        sql = sqlMap.get(sql);
        try {
            String name = user.getUsername();
            ps = ConnectJDBC.connectDB(sql);
            ps.setString(1, user.getUsername());
            rs = ps.executeQuery();
            if (rs.next()) {
                u.setUserID(Integer.parseInt(rs.getString(1)));
                u.setUsername(rs.getString(2));
                u.setPassword(rs.getString(3));
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
        }
        return u;
    }

    @Override
    public void queryUserByName(User user, String sql) throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//       String sql = "INSERT into tb_user (id,username,password) values(null,?,?)";
//       String sql = "INSERT into tb_user (id,username,password) values(null,#{username},#{password})";
        PreparedStatement ps = null;
        sql = sqlMap.get(sql);
        try {
//            String newsql = sql.replaceAll("#\\{[a-zA-Z0-9_$]*}", "?");
//            ps = ConnectJDBC.connectDB(newsql);
              ps = EncapsulationUtil.objectIntoPs(user,sql,ps);
//            int fromIndex = 0;
//            int Index = 1;
//            while(true){
//                int jinIndex = sql.indexOf("#", fromIndex);
//                if(jinIndex < 0)   break;
//                int kuoIndex = sql.indexOf("}", fromIndex);
//                String propertyName = sql.substring(jinIndex + 2, kuoIndex).trim();
//                fromIndex = kuoIndex + 1;
//                String getMethodName = "get" + propertyName.toUpperCase().charAt(0) + propertyName.substring(1);
//                Method getMethod = user.getClass().getDeclaredMethod(getMethodName);
//                Object propertyValue = getMethod.invoke(user);
//                ps.setString(Index,propertyValue.toString());
//                Index++;
//            }
//            ps = ConnectJDBC.connectDB(sql);
//            ps.setString(1, user.getUsername());
//            ps.setString(2, user.getPassword());
              ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e) {
                    throw e;
                }
            }
        }
    }
}
